package com.ruoyi.system.controller;

import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.web.page.TableDataInfo;
import com.ruoyi.system.domain.Pet;
import com.ruoyi.system.domain.dto.PetRecognitionResult;
import com.ruoyi.system.service.IPetService;

/**
 * 宠物信息操作处理
 */
@RestController
@RequestMapping("/pet")
public class PetController extends BaseController {
    @Autowired
    private IPetService petService;

    /**
     * 获取宠物列表
     */
    @GetMapping("/list")
    public TableDataInfo list(Pet pet) {
        startPage();
        List<Pet> list = petService.selectPetList(pet);
        return getDataTable(list);
    }



    /**
     * 新增宠物
     */
    @PostMapping("/add")
    public AjaxResult add(@RequestBody Pet pet) {
        return toAjax(petService.insertPet(pet));
    }

    /**
     * 获取宠物详细信息
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(petService.selectPetById(id));
    }

    /**
     * 修改宠物
     */
    @PutMapping("/edit")
    public AjaxResult edit(@RequestBody Pet pet) {
        return toAjax(petService.updatePet(pet));
    }

    /**
     * 删除宠物
     */
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(petService.deletePetByIds(ids));
    }

    /**
     * 宠物识别
     */
    @PostMapping("/recognize")
    public PetRecognitionResult recognize(@RequestParam("file") MultipartFile file) {
        return petService.recognizePet(file);
    }

    /**
     * 上传文件
     */
    @PostMapping("/upload")
    public AjaxResult uploadFile(@RequestParam("file") MultipartFile file) {

        String fileName = petService.uploadFile(file);
        System.out.println(fileName);
        if(fileName!=null){
            return success(fileName);
        }else{
            return error("图片上传有问题");
        }
    }

    //查询种类
    @GetMapping("/getType")
    public AjaxResult getType(){
        return success(petService.getType());
    }

    //根据种类id查询品种
    @GetMapping("/getPetVariety")
    public AjaxResult getPetVariety(Integer tId){
        return success(petService.getPetVariety(tId));
    }

    @GetMapping("/getPetInfo")
    public AjaxResult getPetInfo(Integer uid){
        return success(petService.getPetInfo(uid));
    }

    //根据图片识别宠物情绪
    @PostMapping("/recognizeEmotion")
    public AjaxResult recognizeEmotion(@RequestParam("file") MultipartFile file){
        return success(petService.recognizeEmotion(file));
    }

    //根据id查询宠物种类
    @GetMapping("/getPetTypeById")
    public AjaxResult getPetTypeById(Long tId){
        return success(petService.getPetTypeById(tId));
    }

    //根据id查询宠物品种
    @GetMapping("/getPetVarietyById")
    public AjaxResult getPetVarietyById(Long vId){
        return success(petService.getPetVarietyById(vId));
    }
}
